package com.jk.goodsclassify.classify.utils;

import com.jk.goodsclassify.classify.entry.Library;
import com.jk.goodsclassify.classify.entry.Location;

import java.io.*;
import java.util.List;
import java.util.Map;

public class OutPutUtil {

    public static void print(Library library){
        String path = new Object() {
            public String getPath() {
                return this.getClass().getResource("/").getPath();
            }
        }.getPath().substring(1)+"library.txt";
        PrintStream ps = null;
        PrintStream original = System.out;
        try {
            ps = new PrintStream(path);
            System.setOut(ps);
            int count = -1;
            for(Map.Entry<String,Integer> entry:library.getData().entrySet()){
                count++;
                System.out.println(count+"-->"+entry.getKey());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            System.setOut(original);
        }
    }

    public static void print(List<Location> locations){
        String path = new Object() {
            public String getPath() {
                return this.getClass().getResource("/").getPath();
            }
        }.getPath().substring(1)+"locations.txt";
        PrintStream ps = null;
        PrintStream original = System.out;
        try {
            ps = new PrintStream(path);
            System.setOut(ps);
            int count = -1;
            for(Location location:locations){
                count++;
                System.out.println(count+"-->"+location.getName()+"-->"+location.getLocation());
            }
            //ps.close();
            System.setOut(System.out);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            System.setOut(original);
        }
    }
}
